package leetcode.code0102;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import leetcode.IDebug;
import leetcode.helper.tree.TreeNode;

public class Solution102 implements IDebug {

	public List<List<Integer>> levelOrder(TreeNode root) {
		List<List<Integer>> ans = new ArrayList<>();
		if (root == null)
			return ans;
		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(root);
		int levelNodeNum = 1;
		while (!queue.isEmpty()) {
			int curLevel = levelNodeNum;
			levelNodeNum = 0;
			List<Integer> list = new ArrayList<>();
			while (curLevel > 0) {
				curLevel--;
				TreeNode cur = queue.poll();
				list.add(cur.val);
				if (cur.left != null) {
					queue.add(cur.left);
					levelNodeNum++;
				}
				if (cur.right != null) {
					queue.add(cur.right);
					levelNodeNum++;
				}
			}
			ans.add(list);
		}
		return ans;
	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

}
